package com.app.controller;

import com.app.entity.AppInfo;
import com.app.entity.DevUser;
import com.app.service.Appinfo.AppinfoService;
import com.app.utils.Constants;
import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/appinfo")
public class AppinfoController {
    private Logger logger= Logger.getLogger(AppinfoController.class);
    @Autowired
    private AppinfoService appinfoService;

    @RequestMapping(value = "useradd",method= RequestMethod.POST)
    public String addUserSave(AppInfo appinfo, HttpSession session, HttpServletRequest request,
                              @RequestParam(value = "a_logoPicPath",required = false) MultipartFile multipartFile) {
        System.out.println("添加---------------------------------------------------------------------------------------------");

        String logoPicPath = null;
        if (!multipartFile.isEmpty()) {
            String path = request.getSession().getServletContext().getRealPath("statics" + File.separator + "uploadfiles");
            logger.info("file  path---------------------------------------->" + path);
            String logoPicPathName = multipartFile.getOriginalFilename();
            logger.info("logoPicPathName--------------------->" + logoPicPathName);
            String prefix = FilenameUtils.getExtension(logoPicPathName);
            logger.debug("siez-------------------------------------------------->" + multipartFile.getSize());
            if (multipartFile.getSize() > 5000000) {
                System.err.println("上传文件大小不能超过50k");
                return "Appadd";
            } else if (prefix.equalsIgnoreCase("jpg") ||
                    prefix.equalsIgnoreCase("png") ||
                    prefix.equalsIgnoreCase("jpeg") ||
                    prefix.equalsIgnoreCase("gif")) {
                //新文件名称  当前系统时间+随机数+_Personal.jpg
                String fileName = System.currentTimeMillis() + Math.random() * 1000000 + "_Personal.jpg";
                logger.debug("fileName-------------------->" + fileName);
                File targetFile = new File(path, fileName);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }
                //保存
                try {
                    multipartFile.transferTo(targetFile);
                } catch (IOException e) {
                    e.printStackTrace();
                    System.err.println("上传失败");
                    return "Appadd";
                }
                logoPicPath = path + File.separator + fileName;
            } else {

                System.err.println("上传图片格式不正确");
                return "Appadd";
            }

        }

       /* BigDecimal xiz=new BigDecimal(request.getParameter("softwareSize"));
        appinfo.setSoftwareSize(xiz);*/
        appinfo.setCreatedBy(((DevUser)session.getAttribute(Constants.USER_SESSION)).getId());
        appinfo.setCreationDate(new Date());
        appinfo.setLogoPicPath(logoPicPath);
     /*   appinfo.setCategoryLevel1(Integer.getInteger(request.getParameter("categoryLevel1")));
        appinfo.setCategoryLevel2(Integer.getInteger(request.getParameter("categoryLevel2")));
        appinfo.setCategoryLevel3(Integer.getInteger(request.getParameter("categoryLevel3")));*/
        appinfo.setStatus(1);
        if (appinfoService.app_info_add(appinfo)){
            return "redirect:/user/show_All";
        }
        return "Appadd";
    }


}
